<?php
class TicketClient
{
    private $host;
    private $api;
    private $code;
    private $username;
    public $thirdUsername = '公司名';
    public function __construct($api = '')
    {
        $this->api      = $api;
        $this->code     = 'drP78cBa';
        $this->username = '13070075714';
        $this->host     = "http://api.51book.com/" . $this->username . "/51";
    }
    public function getUsername()
    {
        return $this->username;
    }

    public function getApiData($rqData)
    {
        //获取13位带毫秒的时间戳
        list($msec, $sec) = explode(' ', microtime());
        $msectime         = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        $postFields       = [
            'rsIsGzip'         => true,
            'rqIdentification' => md5(mt_rand(1,99999999).time()), //用户自定义唯一(请求表示用于bug,查询记录log ; 小于，等于32长度字符串)
            //13位时间戳
            'timeStamp'        => $msectime,
            //第三方名称
            'thirdUsername'    => $this->thirdUsername,
            //业务参数
            'rqData'           => $rqData,
        ];

        $result = $this->curlPost($this->host . $this->api, $postFields);
        //dd($result);
        // echo '<pre>';
        // echo '请求地址<br>';
        // print_r($this->host . $this->api);
        // echo '请求头<br>';
        // print_r($postFields);
        $result = json_decode($result, true);

        return $result;
    }

    //CURL 请求
    public function curlPost($url, $postFields)
    {
        //将传递过来的参数，数组进行json转换
        $postFields = json_encode($postFields);
        //将参数加密，拼接安全码
        $sign = md5($postFields . $this->code);
        //初始化curl
        $ch         = curl_init();
        $curl_array = array(
            CURLOPT_URL            => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING       => "",
            CURLOPT_MAXREDIRS      => 10,
            CURLOPT_TIMEOUT        => 100,
            CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST  => "POST",
            CURLOPT_POSTFIELDS     => "{$postFields}",
            CURLOPT_HTTPHEADER     => array(
                "cache-control: no-cache",
                "content-type: application/json",
                "sign: {$sign}",
                "username: {$this->username}",
            ),
        );

        //组装请求头
        curl_setopt_array($ch, $curl_array);
        //开始请求
        $ret = curl_exec($ch);
        // var_dump($ret);

        if (false == $ret) {
            $result = curl_error($ch);
        } else {
            $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 " . $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
        curl_close($ch);
        //返回数组，json格式
        return $result;
    }

}

// $client = new SoapClient("http://ws.51book.com:8000/ltips/services/getAvailableFlightWithPriceAndCommisionService1.0?wsdl");
// $param = [
//     'agencyCode' => '13070075714',
//     'orgAirportCode' =>'PEK',
//     'dstAirportCode' =>'SHA',
//     'date' =>'2020-06-28',
//     'onlyAvailableSeat' =>0,
//     'onlyNormalCommision' =>1,
//     'onlySelfPNR' =>0,
//     'onlyOnWorkingCommision'=>0
// ];
// $param['sign']=md5($param['agencyCode'].$param['dstAirportCode'].$param['onlyAvailableSeat'].$param['onlyNormalCommision'].$param['onlyOnWorkingCommision']
//     .$param['onlySelfPNR'].$param['orgAirportCode'].'drP78cBa');

//  echo '<pre>';
//  print_r(ArrToXml($param));
// try {
//     $result = $client->getAvailableFlightWithPriceAndCommision(ArrToXml($param));/* 调用方法 */
//     die(json_encode(object_array($result)));
// } catch (SoapFault $f){
//     echo '__getFunctions = ';
//     var_dump($client->__getFunctions());
//     echo '<br/>-----------------------<br/>';
//     echo '__getLastRequest = '.$client->__getLastRequest();
//     echo '<br/>-----------------------<br/>';
//     echo '__getLastResponse = '.$client->__getLastResponse();
//     echo '<br/>-----------------------<br/>';
//     echo "Error Message: {$f->getMessage()}";
// }

// function object_array($array) {
//     if(is_object($array)) {
//         $array = (array)$array;
//     }
//     if(is_array($array)) {
//         foreach($array as $key=>$value) {
//             $array[$key] = object_array($value);
//         }
//     }
//     return $array;
// }
// function ArrToXml($arr)
// {
//     if(!is_array($arr) || count($arr) == 0) return '';

//     $xml = "<xml>";
//     foreach ($arr as $key=>$val)
//     {
//         if (is_numeric($val)){
//             $xml.="<".$key.">".$val."</".$key.">";
//         }else{
//             $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
//         }
//     }
//     $xml.="</xml>";
//     return $xml;
// }
